//
//  RouteTypeTableCell.m
//  Cycling
//
//  Created by 迟国强 on 2023/4/5.
//

#import "RouteTypeTableCell.h"
#import "TypeItemCell.h"

@interface RouteTypeTableCell ()

@property (weak, nonatomic) IBOutlet UILabel *typeLbl;

@end

@implementation RouteTypeTableCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TypeItemCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"TypeItemCell"];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)setEditModel:(CyclingRouteEditModel *)editModel
{
    _groundEditModel = nil;
    _editModel = editModel;
    
    self.typeLbl.text = editModel.detailModel.bicycleType.txt;
    [self.collectionView reloadData];
}

- (void)setGroundEditModel:(CyclingRouteEditModel *)groundEditModel
{
    _editModel = nil;
    _groundEditModel = groundEditModel;
    
    self.typeLbl.text = groundEditModel.detailModel.ground.txt;
    [self.collectionView reloadData];
}

#pragma mark

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TypeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeItemCell" forIndexPath:indexPath];
    
    NSDictionary *cellData = [[self typeModels] objectAtIndex:indexPath.row];
    cell.name = [cellData valueForKey:@"name"];
    NSInteger routeId = [[cellData valueForKey:@"id"] integerValue];
    if(self.editModel) {
        cell.hasSelect = self.editModel.cycleType == routeId;
    } else if(self.groundEditModel) {
        cell.hasSelect = self.groundEditModel.routeType == routeId;
    }
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self typeModels].count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(116, 40);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellData = [[self typeModels] objectAtIndex:indexPath.row];
    if(self.editModel) {
        self.editModel.cycleType = [[cellData valueForKey:@"id"] integerValue];
    } else if(self.groundEditModel) {
        self.groundEditModel.routeType = [[cellData valueForKey:@"id"] integerValue];
    }
    
    [self.collectionView reloadData];
}

- (NSArray *)typeModels
{
    if(self.groundEditModel) {
        return self.groundEditModel.detailModel.ground.data;
    }
    return self.editModel.detailModel.bicycleType.data;
}

@end
